package com.xiang.plane.model;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;

import com.xiang.plane.R;
import com.xiang.plane.util.PVector;
import com.xiang.plane.util.Timer;
import com.xiang.plane.util.TypeConst;
import com.xiang.plane.view.GameView;

import java.util.List;
import java.util.Random;

/**
 * Created by XY on 2017/8/10.
 */

public class Boss {
    private static final String TAG = "Enemy";
    //敌机的图片
    private Bitmap enemyBtm;
    //敌机的坐标
    public int x, y;
    //敌机的宽高
    public int width, height;
    //速度
    private int speedX = 4;
    private int speedY = 4;
    //在y轴移动的距离
    private int distanceY;
    //生命值
    public int life = 50;
    //是否爆炸状态，是否爆炸结束状态
    public boolean isExplode = false, isExplodeDead = false;
    //爆炸半径,最大爆炸半径
    private int explodeRadius = 0, explodeMaxRadius = 100;
    //这里设置一秒钟产生1颗子弹
    private Timer fireTimer = new Timer(20);
    private Timer moveTimer = new Timer(100);

    //贝塞尔曲线的路径点列表
    private List<Point> pointList = null;
    //当前应该取得的路径点索引
    private int pathIndex = 0;
    //敌机的分数
    private int value = 0;

    private Resources res;

    public Boss(Resources res) {
        this.res = res;
        //暂从6中飞机中随机出现
        //创建一个随机数生成器
        Random random = new Random();
        enemyBtm = BitmapFactory.decodeResource(res, R.drawable.bigenemy);
        width = enemyBtm.getWidth();
        height = enemyBtm.getHeight();
        //随机产生一个从0到窗口宽度减敌机宽度之间的整数，不让敌机从最右边看不到的地方出来
        x = GameView.screenW / 2;
        y = -10;
    }

    public void draw(Canvas canvas, Paint paint) {

        //如果爆炸状态开启，就开始画爆炸的效果
        if (this.isExplode) {
            //画爆炸效果
            drawExplodeType(canvas, paint);
        }

        //画敌机
        canvas.drawBitmap(enemyBtm, x, y, paint);
    }

    //根据敌机类型画不同的爆炸效果
    private void drawExplodeType(Canvas canvas, Paint paint) {
        //画的方式是空心圆
        paint.setStyle(Paint.Style.STROKE);
        //设置线宽
        paint.setStrokeWidth(4f);
        paint.setColor(Color.YELLOW);
        //画一个圆圈表示爆炸效果
        canvas.drawCircle(x - 10, y - 10, explodeRadius, paint);
        //每一帧让圆的半径增大15
        this.explodeRadius += 20;
        if (this.explodeRadius > this.explodeMaxRadius) {
            this.isExplodeDead = true;
        }
    }

    public void logic() {
        //每帧追踪敌机
        this.move();
        this.checkDead();
        this.fire();
    }

    //敌机的移动
    private void move() {
        x += speedX;
        if (x + width >= GameView.screenW || x <= 0) {
            speedX = -speedX;
        }
        /**
         * 向下移动
         *      向y轴移动,距离在100到300随机取
         */
        if (moveTimer.act()) {
            Random random = new Random();
            distanceY = random.nextInt(500) + 400;
        }
        if (distanceY > 0) {
            y += speedY;
//            Log.d(TAG, "boss下移  y = " + y);
            if (y >= distanceY) {
                speedY = -speedY;
            }
            if (y <= 0) {
                y = 0;
                distanceY = -1;
                speedY = -speedY;
            }
        }

    }

    public void fire() {
        //产生一颗子弹，位置就在敌人飞机的正前方
        if (this.fireTimer.act()) {
            //这里的+20和+55用来调整子弹的初始位置，让它从飞机的正前方打出来
            Bullet temp = new Bullet(res, this.x + enemyBtm.getWidth() / 2, this.y + enemyBtm.getHeight(),
                    true, TypeConst.ENEMY_BULLET_TRACK_1);
            //改变它的方向向量
            temp.setTargetV(new PVector(GameView.player.getPlayerX() - temp.bulletX + GameView.player.getPlaneWidthHalf(),
                    GameView.player.getPlayerY() - temp.bulletY + GameView.player.getPlaneHeight()).normalize());
        }
    }

    //检测是不是应该被销毁
    private void checkDead() {
        boolean outHeight = this.y > GameView.screenH;
        boolean outWidth = this.x > GameView.screenW;
        //如果高度越界就销毁
        if (outHeight || outWidth) this.destroy();
        //如果爆炸效果结束了，那么也销毁
        if (this.isExplodeDead) this.destroy();
    }

    //受到伤害，掉血
    public void beHit() {
        //如果没有血了，就开始爆炸动画
        if (--this.life < 0) this.isExplode = true;
    }

    //销毁自己，释放资源
    private synchronized void destroy() {
        //计算分数
        GameView.Score += value;
        GameView.enemyVector.remove(this);
        //让图像到窗口外面去
        this.y = 10000;
        this.x = 10000;
    }

}
